package cn.shenlong.front.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.shenlong.portal.domain.Advertisement;
import cn.shenlong.portal.service.AdvertisementService;
import cn.shenlong.util.DirectiveUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component("cms_banner_list")
public class BannerDirective implements
TemplateDirectiveModel {
	@Autowired
	private AdvertisementService advertisementService;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		 
	 
		List<Advertisement> list = advertisementService.findAllAdv();
		
		List<Advertisement> homeList = new ArrayList<Advertisement>();
		List<Advertisement>  cardList = new ArrayList<Advertisement>();
		List<Advertisement>  advisorsList = new ArrayList<Advertisement>();
		List<Advertisement>  directoriesList = new ArrayList<Advertisement>();
		 
		for (int i = 0; i < list.size(); i++) {
			if(i<3){
				homeList.add(list.get(i)); 
			}else if(i==3){
				cardList.add(list.get(i));
			}else if(3<i&&i<6){
				advisorsList.add(list.get(i));
			}else{
				directoriesList.add(list.get(i));
			}
			
		}
 
		env.setVariable("home_banner_list", DEFAULT_WRAPPER.wrap(homeList));
		env.setVariable("card_banner_list", DEFAULT_WRAPPER.wrap(cardList));
		env.setVariable("advisors_banner_list", DEFAULT_WRAPPER.wrap(advisorsList));
		env.setVariable("directories_banner_list", DEFAULT_WRAPPER.wrap(directoriesList));
		body.render(env.getOut());
	}
	
	
}
